input {
    file {
        type => "nginx_access_log"
        path => ["/var/log/nginx/*_access.log"]
        start_position => "beginning"
        exclude => "*.gz"
    }

     file {
            type => "nginx_error_log"
            path => ["/var/log/nginx/*_error.log"]
            start_position => "beginning"
            exclude => "*.gz"
     }
}

filter {

    if [type] == "nginx_access_log" {
        grok {
            patterns_dir => "/etc/logstash/patterns.d"
            match => {
                "message" => "%{NGINX_ACCESS_LOG}"
            }
        }
    }

    if [type] == "nginx_error_log" {
        grok {
            patterns_dir => "/etc/logstash/patterns.d"
            match => {
                "message" => "%{NGINX_ERROR_LOG}"
            }
        }
    }

    mutate {
            remove_field =>["message"]
            remove_field =>["@timestamp"]
            remove_field =>["path"]
    }
}

output {
    if [type] == "nginx_access_log" {
        elasticsearch {
            hosts => ["{{ monitor_ip }}:9200"]
            index => "log-nginx-access"
        }
    }

    if [type] == "nginx_error_log" {
        elasticsearch {
            hosts => ["{{ monitor_ip }}:9200"]
            index => "log-nginx-error"
        }
    }
}
